package com.hdculture.simple.expensemanager.premium;

import java.text.DecimalFormat;
import java.util.Calendar;
import java.util.Date;

import com.hdculture.simple.expensemanager.premium.R;

import android.app.Activity;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;
import android.util.Log;
import android.view.*;
import android.os.Bundle;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Color;

public class ManageIncomeActivity extends Activity {
    /** Called when the activity is first created. */
	Button backButton;
	TextView lastMonthIncome, thisMonthIncome, thisMonthText, lastMonthText, IRiseFall;
	CoreDBAdapter dbAdapter;
	String currencySign="$";
	ListView choiceList;
	String tag = "ManageIncomeActivity";
		String[] choices = {"Income Sources","Income by Month","All Income"};

		@Override
		protected void onPause() {
			// TODO Auto-generated method stub
	    	dbAdapter.close();
	    	Log.i(tag,"DB Connection Closed");
			super.onPause();
		}
		@Override
		protected void onResume() {
			// TODO Auto-generated method stub
			dbAdapter.open();
			Log.i(tag,"DB Connection Resumed");
			super.onResume();
		}
		
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.manageincome);
        lastMonthIncome = (TextView)findViewById(R.id.lastMonthIncomeText);
        lastMonthText = (TextView)findViewById(R.id.lastMonthText);
        thisMonthIncome = (TextView)findViewById(R.id.thisMonthIncomeText);
        thisMonthText = (TextView)findViewById(R.id.thisMonthText);
        IRiseFall = (TextView)findViewById(R.id.IRiseFall);
        dbAdapter = new CoreDBAdapter(ManageIncomeActivity.this);
        dbAdapter.open();
        Log.i(tag,"DB Connection Opened");
        currencySign = dbAdapter.getCurrencySign();
        backButton = (Button)findViewById(R.id.backButton);
        	manageBackButtonClickEvent();
        choiceList = (ListView)findViewById(R.id.choiceList);
        	initializeChoiceList();
        	updateTotalIncome();
    }
    public void manageBackButtonClickEvent(){
    	backButton.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				dbAdapter.close();
				finish();
			}
		});
    }
    public void initializeChoiceList(){
    	choiceList.setAdapter(new ArrayAdapter<String>(ManageIncomeActivity.this,android.R.layout.simple_list_item_1,choices));
    	manageChoiceListClickEvent();
    }
    
    public void updateTotalIncome(){
    	double thisMonthIncomeDbl=0.00;
    	double lastMonthIncomeDbl=0.00;
    	int thisMonth = Calendar.getInstance().get(Calendar.MONTH)+1;
    	int lastMonth = thisMonth-1;
    	if(thisMonth==1){
    		lastMonth=12;
    	}
    	thisMonthText.setText(getMonthName(thisMonth)+":");
    	lastMonthText.setText(getMonthName(lastMonth)+":");
    	Cursor c = dbAdapter.fetchTotalIncomeThisMonth();
    	startManagingCursor(c);
    	c.moveToFirst();
    	if(c.isAfterLast()==false){
    		String incomeString="";
    		DecimalFormat currencyFormat = new DecimalFormat(currencySign+"##,###.00");
    		thisMonthIncomeDbl = c.getDouble(0);
    		incomeString = currencyFormat.format(thisMonthIncomeDbl);
    		thisMonthIncome.setText(incomeString);
    	}
    	c = dbAdapter.fetchTotalIncomeLastMonth();
    	startManagingCursor(c);
    	c.moveToFirst();
    	if(c.isAfterLast()==false){
    		String incomeString="";
    		DecimalFormat currencyFormat = new DecimalFormat(currencySign+"##,###.00");
    		lastMonthIncomeDbl=c.getDouble(0);
    		incomeString = currencyFormat.format(lastMonthIncomeDbl);
    		lastMonthIncome.setText(incomeString);
    	}
    	double iPercent = ((thisMonthIncomeDbl-lastMonthIncomeDbl)/lastMonthIncomeDbl)*100;
    	DecimalFormat percentDigit = new DecimalFormat("#0.0");
    	IRiseFall.setText("  "+percentDigit.format(iPercent)+"%");
    	if(iPercent<0){
    		IRiseFall.setTextColor(Color.RED);
    	}else if(iPercent>5){
    		IRiseFall.setTextColor(Color.GREEN);
    	}
    }
    
    public String getMonthName(int month){
    	String monthString = "";
        switch(month){
        case 1:	monthString = "January";break;
        case 2: monthString = "February";break;
        case 3: monthString = "March";break;
        case 4: monthString = "April";break;
        case 5: monthString = "May";break;
        case 6: monthString = "June";break;
        case 7: monthString = "July";break;
        case 8: monthString = "August";break;
        case 9: monthString = "September";break;
        case 10: monthString = "October";break;
        case 11: monthString = "November";break;
        case 12: monthString = "December";break;
        }
        return monthString;
    }
    
    public void manageChoiceListClickEvent(){
    	choiceList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				switch(arg2){
				case 0:	//Income sources
						Intent incomeSourceActivity = new Intent(ManageIncomeActivity.this,ManageIncomeActivity_IncomeSources.class);
						startActivity(incomeSourceActivity);
						break;
				case 1: //Income by Month
						Intent monthSelectorActivity = new Intent(ManageIncomeActivity.this,MonthSelector.class);
						monthSelectorActivity.putExtra("ISINCOME", true);
						startActivity(monthSelectorActivity);
						break;
				case 2: Intent allIncomeActivity = new Intent(ManageIncomeActivity.this,AllIncomeActivity.class);
						startActivity(allIncomeActivity);
						break;
				}
			}
		});
    }
}